package com.dc.ftp.activity.loan;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dc.ftp.R;
import com.dc.ftp.base.SPBaseActivity;
import com.dc.ftp.utils.PickerViewUtil;

import java.util.ArrayList;

public class ConnectPersonInfoActivity extends SPBaseActivity implements View.OnClickListener{

    private View parentView;
    private TextView tvRelationA;
    private TextView tvRelationB;
    private TextView tvRelationC;
    private ArrayList<String> mList;
    private ArrayList<String> mList2;
    private TextView tvPhoneA;
    private TextView tvPhoneB;
    private TextView tvPhoneC;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_person_info);
        super.init();
        setHeadTitle(true,"联系人信息");
    }

    @Override
    public void initView() {
        parentView = findViewById(R.id.activity_connect_person_info);

        findViewById(R.id.rly_choose_relation_a).setOnClickListener(this);
        findViewById(R.id.rly_choose_relation_b).setOnClickListener(this);
        findViewById(R.id.rly_choose_relation_c).setOnClickListener(this);

        tvRelationA = (TextView) findViewById(R.id.tv_relation_a);
        tvRelationB = (TextView) findViewById(R.id.tv_relation_b);
        tvRelationC = (TextView) findViewById(R.id.tv_relation_c);

        findViewById(R.id.lly_choose_phone_a).setOnClickListener(this);
        findViewById(R.id.lly_choose_phone_b).setOnClickListener(this);
        findViewById(R.id.lly_choose_phone_c).setOnClickListener(this);

        tvPhoneA = (TextView) findViewById(R.id.tv_phone_a);
        tvPhoneB = (TextView) findViewById(R.id.tv_phone_b);
        tvPhoneC = (TextView) findViewById(R.id.tv_phone_c);
    }

    @Override
    public void initData() {
        mList = new ArrayList<>();
        mList.add("父亲");
        mList.add("母亲");
        mList.add("配偶");
        mList.add("朋友");
        mList2 = new ArrayList<>();//保存选择后list变化后的选项数据

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rly_choose_relation_a:
                resetList();
                PickerViewUtil.initPickerView(this,parentView,false,"选择关系",mList2,tvRelationA);
                break;
            case R.id.rly_choose_relation_b:
                resetList();
                PickerViewUtil.initPickerView(this,parentView,false,"选择关系",mList2,tvRelationB);
                break;
            case R.id.rly_choose_relation_c:
                resetList();
                PickerViewUtil.initPickerView(this,parentView,false,"选择关系",mList2,tvRelationC);
                break;


            case R.id.lly_choose_phone_a:
                startActivityForResult(new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI), 0);
                break;
            case R.id.lly_choose_phone_b:
                startActivityForResult(new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI), 0);
                break;
            case R.id.lly_choose_phone_c:
                startActivityForResult(new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI), 0);
                break;
            default:break;
        }

    }


    public void resetList(){
        String a = tvRelationA.getText().toString();
        String b = tvRelationB.getText().toString();
        String c = tvRelationC.getText().toString();
        mList2.clear();
        for (int i=0;i<mList.size();i++){
            if (!mList.get(i).equals(a)&&!mList.get(i).equals(b)&&!mList.get(i).equals(c)){
                String s = mList.get(i);
                mList2.add(s);

            }
        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Activity.RESULT_CANCELED){return;}

        if (resultCode == Activity.RESULT_OK) {
            // ContentProvider展示数据类似一个单个数据库表
            // ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
            ContentResolver reContentResolverol = getContentResolver();
            // URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
            Uri contactData = data.getData();
            // 查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            // 条件为联系人ID
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            // 获得DATA表中的电话号码，条件为联系人ID,因为手机号码可能会有多个
            Cursor phone = reContentResolverol.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                            + contactId, null, null);
            while (phone.moveToNext()) {
                String usernumber = phone
                        .getString(phone
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                tvPhoneA.setText(usernumber);
            }

        }
    }
}
